<ion-header>
    <ion-toolbar>
        <ion-buttons slot="start">
            <ion-back-button [text]="'core.back' | translate" />
        </ion-buttons>
        <ion-title>
            <h1>
                @if (title) {
                    <core-format-text [text]="title" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId" />
                }
            </h1>
        </ion-title>
        <ion-buttons slot="end" [hidden]="!loaded">
            @if (assessmentId && access.assessingallowed) {
                <ion-button fill="clear" (click)="saveAssessment()">
                    {{ 'core.save' | translate }}
                </ion-button>
            }
            @if (canAddFeedback && !loadFeedbackToEditErrorMessage) {
                <ion-button fill="clear" (click)="saveEvaluation()">
                    {{ 'core.save' | translate }}
                </ion-button>
            }
        </ion-buttons>
    </ion-toolbar>
</ion-header>
<ion-content class="limited-width">
    @if (!((assessmentId && access.assessingallowed) || canAddFeedback)) {
        <ion-refresher slot="fixed" [disabled]="!loaded" (ionRefresh)="refreshSubmission($event.target)">
            <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}" />
        </ion-refresher>
    }
    <core-loading [hideUntil]="loaded">
        @if (submission) {
            <ion-list>
                <addon-mod-workshop-submission [submission]="submission" [courseId]="courseId" [module]="module" [workshop]="workshop"
                    [access]="access" [submissionPage]="true" />
                @if (canEdit || canDelete) {
                    <ion-item class="ion-text-wrap">
                        <ion-label>
                            @if (canEdit) {
                                <ion-button expand="block" (click)="editSubmission()">
                                    <ion-icon name="fas-pen" slot="start" aria-hidden="true" />
                                    {{ 'addon.mod_workshop.editsubmission' | translate }}
                                </ion-button>
                            }
                            @if (!submission.deleted && canDelete) {
                                <ion-button expand="block" color="danger" (click)="deleteSubmission()">
                                    <ion-icon name="fas-trash" slot="start" aria-hidden="true" />
                                    {{ 'addon.mod_workshop.deletesubmission' | translate }}
                                </ion-button>
                            }
                            @if (submission.deleted && canDelete) {
                                <ion-button expand="block" fill="outline" color="danger" (click)="undoDeleteSubmission()">
                                    <ion-icon name="fas-rotate-left" slot="start" aria-hidden="true" />
                                    {{ 'core.restore' | translate }}
                                </ion-button>
                            }
                        </ion-label>
                    </ion-item>
                }
            </ion-list>
        }

        @if (!canAddFeedback && evaluate?.text) {
            <ion-list>
                <ion-item class="ion-text-wrap">
                    @if (evaluateByProfile) {
                        <core-user-avatar [user]="evaluateByProfile" slot="start" [courseId]="courseId" [userId]="evaluateByProfile.id" />
                    }
                    <ion-label>
                        @if (evaluateByProfile && evaluateByProfile.fullname) {
                            <h2>
                                {{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateByProfile.fullname} }}
                            </h2>
                        }
                        <core-format-text [text]="evaluate?.text" contextLevel="module" [contextInstanceId]="module.id"
                            [courseId]="courseId" />
                    </ion-label>
                </ion-item>
            </ion-list>
        }

        @if (ownAssessment && !assessment) {
            <ion-list>
                <ion-item-divider class="ion-text-wrap">
                    <ion-label>
                        <h2>{{ 'addon.mod_workshop.yourassessment' | translate }}</h2>
                    </ion-label>
                </ion-item-divider>
                <addon-mod-workshop-assessment [submission]="submission" [assessment]="ownAssessment" [courseId]="courseId"
                    [access]="access" [module]="module" [workshop]="workshop" />
            </ion-list>
        }

        @if (submissionInfo && submissionInfo.reviewedby && submissionInfo.reviewedby.length && !assessment) {
            <ion-list>
                <ion-item-divider class="ion-text-wrap">
                    <ion-label>
                        <h2>{{ 'addon.mod_workshop.receivedgrades' | translate }}</h2>
                    </ion-label>
                </ion-item-divider>
                <ng-container *ngFor="let reviewer of submissionInfo.reviewedby">
                    @if (!reviewer.ownAssessment) {
                        <addon-mod-workshop-assessment [submission]="submission" [assessment]="reviewer" [courseId]="courseId"
                            [access]="access" [module]="module" [workshop]="workshop" />
                    }
                </ng-container>
            </ion-list>
        }

        @if (submissionInfo && submissionInfo.reviewerof && submissionInfo.reviewerof.length && !assessment) {
            <ion-list>
                <ion-item-divider class="ion-text-wrap">
                    <ion-label>
                        <h2>{{ 'addon.mod_workshop.givengrades' | translate }}</h2>
                    </ion-label>
                </ion-item-divider>
                <addon-mod-workshop-assessment *ngFor="let reviewer of submissionInfo.reviewerof" [submission]="submission"
                    [assessment]="reviewer" [courseId]="courseId" [module]="module" [workshop]="workshop" [access]="access" />
            </ion-list>
        }

        @if (canAddFeedback && submission) {
            @if (loadFeedbackToEditErrorMessage) {
                <!-- Error loading the feedback to edit it, display a warning. -->
                <ion-item-divider class="ion-text-wrap">
                    <ion-label>
                        <h2>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</h2>
                    </ion-label>
                </ion-item-divider>
                <ion-card class="core-warning-card">
                    <ion-item>
                        <ion-icon name="fas-triangle-exclamation" slot="start" aria-hidden="true" />
                        <ion-label>{{ loadFeedbackToEditErrorMessage }}</ion-label>
                    </ion-item>
                </ion-card>
            } @else {
                <form [formGroup]="feedbackForm" #feedbackFormEl>
                    <ion-item-divider class="ion-text-wrap">
                        <ion-label>
                            <h2>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</h2>
                        </ion-label>
                    </ion-item-divider>
                    @if (access.canpublishsubmissions) {
                        <ion-item class="ion-text-wrap">
                            <ion-toggle formControlName="published">
                                <p class="item-heading">{{ 'addon.mod_workshop.publishsubmission' | translate }}</p>
                                <p>{{ 'addon.mod_workshop.publishsubmission_help' | translate }}</p>
                            </ion-toggle>
                        </ion-item>
                    }

                    <ion-item-divider class="ion-text-wrap">
                        <ion-label>
                            <h3>{{ 'addon.mod_workshop.gradecalculated' | translate }}</h3>
                            <p>{{ submission.grade }}</p>
                        </ion-label>
                    </ion-item-divider>
                    <ion-item class="ion-text-wrap">
                        <ion-select labelPlacement="stacked" formControlName="grade" interface="action-sheet"
                            [cancelText]="'core.cancel' | translate"
                            [interfaceOptions]="{header: 'addon.mod_workshop.gradeover' | translate}"
                            [label]="'addon.mod_workshop.gradeover' | translate">
                            <ion-select-option *ngFor="let grade of evaluationGrades" [value]="grade.value">
                                {{grade.label}}
                            </ion-select-option>
                        </ion-select>
                    </ion-item>
                    <ion-item>
                        <ion-label position="stacked">{{ 'addon.mod_workshop.feedbackauthor' | translate }}</ion-label>
                        <core-rich-text-editor [control]="feedbackForm.controls['text']" name="text" [autoSave]="true" contextLevel="module"
                            [contextInstanceId]="module.id" elementId="feedbackauthor_editor" [draftExtraParams]="{id: submissionId}" />
                    </ion-item>
                </form>
            }
        }

        @if (assessmentId) {
            <addon-mod-workshop-assessment-strategy [workshop]="workshop" [access]="access" [assessmentId]="assessmentId"
                [userId]="assessmentUserId" [strategy]="strategy" [edit]="access.assessingallowed" />

            @if (!access.assessingallowed && assessment?.feedbackreviewer && workshop.phase === phaseClosed) {
                <ion-list>
                    <ion-item class="ion-text-wrap">
                        @if (evaluateGradingByProfile) {
                            <core-user-avatar [user]="evaluateGradingByProfile" slot="start" [courseId]="courseId"
                                [userId]="evaluateGradingByProfile.id" />
                        }
                        <ion-label>
                            @if (evaluateGradingByProfile && evaluateGradingByProfile.fullname) {
                                <h3 class="item-heading">
                                    {{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateGradingByProfile.fullname} }}
                                </h3>
                            }
                            <core-format-text [text]="assessment!.feedbackreviewer" contextLevel="module" [contextInstanceId]="module.id"
                                [courseId]="courseId" />
                        </ion-label>
                    </ion-item>
                </ion-list>
            }
        }
    </core-loading>
</ion-content>
